﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;

namespace Sunboxsoft.ASMS.Client.UI.Management
{
    /// <summary>
    /// 车辆列表
    /// </summary>
    public partial class AutomobileListView : UserControl, IView
    {
        Promise _promise = new Promise();
        WebRef.VCustomer _customer = null;
        public AutomobileListView()
        {
            InitializeComponent();
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection
                .AddAction(new ActionNode("view", Resources.isView, "查看", ctlBtnView_Click))
                .AddAction(new ActionNode("create", Resources.isCreateCar, "新增", ctlBtnCreate_Click))
                .AddAction(new ActionNode("edit", Resources.isEdit, "修改", ctlBtnEdit_Click))
                 .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", btnRefresh_Click));
        }
        public void Resume(ViewResumeArgs args)
        {
            if (args.ClosedArgs != null && args.ClosedArgs.Result)
            {
                if (args.ClosedArgs.View is Pickers.VCustomerAccuratelyPicker)
                {
                    _customer = args.ClosedArgs.Data as WebRef.VCustomer;
                    ctlEditCustomer.Text = _customer.Name;
                    LoadAutomobileListData();
                }
                else if (args.ClosedArgs.View is Create.CreateAutomobileView ||
                    args.ClosedArgs.View is Edit.UpdateBookingAutoMobileView)
                {
                    LoadAutomobileListData();
                }
            }
        }
        public void Active(ViewActiveArgs args)
        {
            _promise.Then(LoadAutomobileListData).Run();
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("车辆列表");
        }
        #endregion

        #region Action
        /// <summary>
        /// 查看车辆详细信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnView_Click(object sender, object e)
        {
            if (_customer == null)
            {
                return;
            }
            if (this.GVData.FocusedRowHandle > -1)
            {
                WebRef.VAutomobile item = this.GVData.GetFocusedRow() as WebRef.VAutomobile;
                if (item != null)
                {
                    UIContext.Current.ViewManager.PopupView(new Show.ShowAutoMobileDetailView(_customer, item), new PopupViewArgs("查看车辆信息"));
                }
            }
        }
        /// <summary>
        /// 新增车辆信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnCreate_Click(object sender, object e)
        {
            if (_customer == null)
            {
                UIContext.Current.ViewManager.ShowInfo("请选择客户！");
                return;
            }
            UIContext.Current.ViewManager.PopupView(new Create.CreateAutomobileView(_customer), new PopupViewArgs("新增车辆资料") { TagCode = 0x02 });
        }

        /// <summary>
        /// 修改车辆详细信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnEdit_Click(object sender, object e)
        {
            if (_customer == null)
            {
                UIContext.Current.ViewManager.ShowInfo("请选择客户！");
                return;
            }
            if (this.GVData.FocusedRowHandle > -1)
            {
                WebRef.VAutomobile item = this.GVData.GetFocusedRow() as WebRef.VAutomobile;
                if (item != null)
                {
                    UIContext.Current.ViewManager.PopupView(new Edit.UpdateBookingAutoMobileView(_customer, item), new PopupViewArgs("修改车辆信息"));
                }
            }
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, object e)
        {
            LoadAutomobileListData();
        }
        #endregion

        #region methods
        private void LoadAutomobileListData()
        {
            if (_customer == null)
            {
                return;
            }
            UIContext.Current.InvokeAsync(() =>
            {
                ListResult<WebRef.VAutomobile> result = new Service.QueryService().QueryAutomobileList(_customer.Id);
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        ctlGrid.DataSource = result.Data.Items;
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                    }
                }));
            });
        }
        /// <summary>
        /// 选择客户&清除客户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlEditCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.Equals("clear"))
            {
                this.ctlEditCustomer.Text = "";
                _customer = null;
            }
            else
            {
                UIContext.Current.ViewManager.PopupView(new Pickers.VCustomerAccuratelyPicker(), new PopupViewArgs("选择客户"));
            }
        }
        #endregion
    }
}
